class Solution:
    def hasAlternatingBits(self, n: int) -> bool:
        if n <= 2:
            return True
        mark = None
        for i in range(32):
            temp = 2**i
            if n == temp:
                return False
            if n < temp:
                break
            res = 1 if temp & n == temp else 0
            if mark == res:
                return False
            mark = res

        return True
